package org.edu.modules.jxgz.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxgz.api.CommonApi;
import org.edu.modules.jxgz.entity.VeJwGzzj;
import org.edu.modules.jxgz.entity.VeJwJxrz;
import org.edu.modules.jxgz.entity.VeJwText;
import org.edu.modules.jxgz.service.IVeJwJxrzService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.jxgz.util.VeJwTextUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: ve_jw_jxrz
 * @Author: jeecg-boot
 * @Date:   2021-05-26
 * @Version: V1.0
 */
@Api(tags="ve_jw_jxrz")
@RestController
@RequestMapping("/jxgz/veJwJxrz")
@Slf4j
public class VeJwJxrzController extends BaseController<VeJwJxrz, IVeJwJxrzService> {
	@Autowired
	private IVeJwJxrzService veJwJxrzService;
	 @Autowired
	 private CommonApi commonApi;

	 /**
	  * 常量
	  */
	 //未评审
	 private static final Integer NOT_REVIEWED = 0;
	 //已评审
	 private static final Integer REVIEWED = 1;

	/**
	 * 分页列表查询
	 *
	 * @param veJwJxrz
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ve_jw_jxrz-分页列表查询")
	@ApiOperation(value="ve_jw_jxrz-分页列表查询", notes="ve_jw_jxrz-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(VeJwJxrz veJwJxrz,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VeJwJxrz> queryWrapper = QueryGenerator.initQueryWrapper(veJwJxrz, req.getParameterMap());
		Page<VeJwJxrz> page = new Page<VeJwJxrz>(pageNo, pageSize);
		IPage<VeJwJxrz> pageList = veJwJxrzService.page(page, queryWrapper);
		List<VeJwJxrz> records = pageList.getRecords();
		for (VeJwJxrz veJwJxrzTemp : records) {
			VeJwText veJwText = VeJwTextUtils.getVeJwTextById(veJwJxrzTemp.getRznrtid());
			if (veJwText != null) {
				veJwJxrzTemp.setRznrtidText(veJwText.getText());
			}
			veJwText = VeJwTextUtils.getVeJwTextById(veJwJxrzTemp.getVerifytid());
			if(veJwText !=null){
				veJwJxrzTemp.setVerifytidText(veJwText.getText());
			}
		}
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param veJwJxrz
	 * @return
	 */
	@AutoLog(value = "ve_jw_jxrz-添加")
	@ApiOperation(value="ve_jw_jxrz-添加", notes="ve_jw_jxrz-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwJxrz veJwJxrz) {
		String rznrtidText = veJwJxrz.getRznrtidText();
		if (StringUtils.isBlank(rznrtidText)) {
			return Result.error("内容不可为空");
		}
		if (veJwJxrz.getAdduser() == null) {
			veJwJxrz.setAdduser(VeJwTextUtils.getLoginUserId());
		}
		Result<JSONObject> jsonObjectResult = commonApi.queryAppUserByUserId(CommonApi.COMMON_TOKEN_KEY, veJwJxrz.getAdduser());
		if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
			JSONObject result = jsonObjectResult.getResult();
			veJwJxrz.setAddusername(result.getString("userName"));
		}else {
			return Result.error("用户信息获取失败");
		}
		veJwJxrz.setVerifytype(NOT_REVIEWED);
		veJwJxrz.setRznrtid(VeJwTextUtils.add(rznrtidText));
		veJwJxrzService.save(veJwJxrz);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param veJwJxrz
	 * @return
	 */
	@AutoLog(value = "ve_jw_jxrz-编辑")
	@ApiOperation(value="ve_jw_jxrz-编辑", notes="ve_jw_jxrz-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwJxrz veJwJxrz) {
		String rznrtidText = veJwJxrz.getRznrtidText();
		if (StringUtils.isBlank(rznrtidText)) {
			return Result.error("内容不可为空");
		}
		VeJwTextUtils.update(veJwJxrz.getRznrtid(),rznrtidText);
		veJwJxrzService.updateById(veJwJxrz);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ve_jw_jxrz-通过id删除")
	@ApiOperation(value="ve_jw_jxrz-通过id删除", notes="ve_jw_jxrz-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		veJwJxrzService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ve_jw_jxrz-批量删除")
	@ApiOperation(value="ve_jw_jxrz-批量删除", notes="ve_jw_jxrz-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.veJwJxrzService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ve_jw_jxrz-通过id查询")
	@ApiOperation(value="ve_jw_jxrz-通过id查询", notes="ve_jw_jxrz-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwJxrz veJwJxrz = veJwJxrzService.getById(id);
		if(veJwJxrz==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(veJwJxrz);
	}


	 /**
	  *  评审
	  *
	  * @param veJwJxrz
	  * @return
	  */
	 @AutoLog(value = "ve_jw_gzzj-评审")
	 @ApiOperation(value="ve_jw_gzzj-评审", notes="ve_jw_gzzj-评审")
	 @PutMapping(value = "/audit")
	 public Result<?> audit(@RequestBody VeJwJxrz veJwJxrz) {
		 try {
			 Result<JSONObject> jsonObjectResult = commonApi.queryAppUserByUserId(CommonApi.COMMON_TOKEN_KEY, veJwJxrz.getVerifyuser());
			 if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
				 JSONObject result = jsonObjectResult.getResult();
				 veJwJxrz.setVerifyusername(result.getString("userName"));
			 }else {
				 return Result.error("用户信息获取失败");
			 }
		 } catch (Exception e) {
			 return Result.error("用户信息获取失败");
		 }
		 if( StringUtils.isNotBlank(veJwJxrz.getVerifytidText())){
			 veJwJxrz.setVerifytid(VeJwTextUtils.addOrUpdate(veJwJxrz.getVerifytid(),veJwJxrz.getVerifytidText()));
		 }
		 veJwJxrz.setVerifytime(new Date());
		 veJwJxrz.setVerifytype(REVIEWED);
		 veJwJxrzService.updateById(veJwJxrz);
		 return Result.ok("编辑成功!");
	 }




	 /**
    * 导出excel
    *
    * @param request
    * @param veJwJxrz
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwJxrz veJwJxrz) {
        return super.exportXls(request, veJwJxrz, VeJwJxrz.class, "ve_jw_jxrz");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwJxrz.class);
    }

}
